<template>
  <el-form
    ref="ruleFormRefUser"
    label-position="left"
    :model="userLoginFrom"
    status-icon
    :rules="rules"
    label-width="70px"
    class="demo-ruleForm"
  >
    <el-form-item label="输入邮箱" prop="email">
      <el-input
        v-model="userLoginFrom.email"
        type="email"
        placeholder="请确认邮箱"
        autocomplete="off"
        :prefix-icon="Message"
      />
    </el-form-item>
    <el-form-item label="输入密码" prop="password" v-if="!isShowForget">
      <el-input
        v-model="userLoginFrom.password"
        type="password"
        placeholder="请输入密码"
        autocomplete="off"
        :prefix-icon="Lock"
      />
    </el-form-item>

    <el-form-item label="验 证 码" prop="code" v-if="isShowForget">
      <el-input
        v-model="userLoginFrom.code"
        placeholder="请输入验证码"
        autocomplete="off"
        :prefix-icon="Key"
      />
    </el-form-item>

    <el-form-item v-if="isShowForget">
      <SendCode :email="userLoginFrom.email"></SendCode>
    </el-form-item>

    <el-form-item>
      <div class="submitBtn">
        <el-button @click="submitForm(ruleFormRefUser)">确认</el-button>
        <el-button @click="resetForm(ruleFormRefUser)">Reset</el-button>
      </div>
    </el-form-item>

    <el-form-item>
      <p class="forget">
        <span @click="forgetHandle">{{
          isShowForget ? '返回登录' : '忘记密码？'
        }}</span>
      </p>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { ref, reactive, defineAsyncComponent, inject } from 'vue'
import { Lock, Message, Key } from '@element-plus/icons-vue'
import type { FormInstance } from 'element-plus'

import { ElMessage } from 'element-plus'

// 验证
import {
  validatePass,
  validateEmail,
  validateCode,
} from '@/hooks/login/validate'

// 登录请求
import { loginUser } from '@/utils/http/login'

const SendCode = defineAsyncComponent(
  () => import('@/components/login/SendCode.vue')
)
const userLoginFrom = reactive({
  password: '',
  email: '',
  code: '',
})
const rules = reactive({
  password: [{ validator: validatePass, trigger: 'blur' }],
  email: [{ validator: validateEmail, trigger: 'blur' }],
  code: [{ validator: validateCode, trigger: 'blur' }],
})

const ruleFormRefUser = ref<FormInstance>()

// 清空表单
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
// 引入全局状态
const store: any = inject('store')
// 提交
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      let res = await loginUser(
        userLoginFrom.email,
        userLoginFrom.password,
        userLoginFrom.code
      )

      res.data.ok
        ? ElMessage.success(`登录成功，欢迎${1}`)
        : ElMessage.error(res.data.info)

      res.data.ok && formEl.resetFields()
      res.data.ok && store.SetUserBase(res.data.info)
    } else {
      return false
    }
  })
}

const isShowForget = ref<boolean>(false)
const forgetHandle = () => {
  isShowForget.value = !isShowForget.value
  ruleFormRefUser.value && ruleFormRefUser.value.resetFields()
}
</script>
<style lang="scss" scoped>
.forget {
  width: 100%;
  font-size: 12px;
  color: #333;
  text-align: right;
  span {
    cursor: pointer;
  }
}

@media (max-width: 760px) {
  .demo-ruleForm {
    :deep(.el-form-item__label) {
      color: #fff;
    }
  }
  .forget {
    color: #fff;
  }
}
</style>
